package leo.manager
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.system.ApplicationDomain;
	import flash.utils.Dictionary;
	
	import leo.data.GameModel;
	/**
	 * 位图管理器 
	 * @author leo
	 * 
	 */
	public class BitmapManager
	{
		private static var _instance :BitmapManager = null;
		public function BitmapManager()
		{
		}
		
		/**
		 * 位图切割成小位图数组   
		 * @param source 源
		 * @param width 宽
		 * @param height 高
		 * @param row 行
		 * @param start 起始列
		 * @param end 结束列
		 * @return 
		 * 
		 */		
		public function sparateBitmapData(source :BitmapData,width :int,height :int,row :int = 0,start :int = 0, end :int = 0):Vector.<BitmapData>
		{
			var result :Vector.<BitmapData> = new Vector.<BitmapData>();
			
			for(var i :int = start; i < end; i++)
			{
				var bitmapData :BitmapData = new BitmapData(width,height,true,0);
				bitmapData.lock();
				bitmapData.copyPixels(source,new Rectangle(i * width, row * height,width,height),new Point());
				bitmapData.unlock();
				result.push(bitmapData);
			}
			
			return result;
		}
		
		
		/**
		 * 左右旋转bitmapData 
		 * @param sourceList
		 * @return 
		 * 
		 */		
		public function rotationBitmapData(sourceList :Vector.<BitmapData>):Vector.<BitmapData>
		{
			if(!sourceList || sourceList.length == 0)
			{
				trace("sourceList null");
				return null;
			}
			var result :Vector.<BitmapData> = new Vector.<BitmapData>();
			for each(var elem :BitmapData in sourceList)
			{
				var newBMD :BitmapData = new BitmapData(elem.width,elem.height,true,0);
				for(var row :int = 0; row < elem.width; row++)
				{
					for(var col :int = 0; col < elem.height; col++)
					{
						newBMD.lock();
						newBMD.setPixel32(elem.width - row,col,elem.getPixel32(row,col));
						newBMD.unlock();
					}
				}
				result.push(newBMD);
			}
			
			return result;
		}
		/**
		 * 获取位图信息
		 * @param name
		 * @return BitmapData
		 * 
		 */		
		public function getBitmapdataByName(name :String):BitmapData
		{
			var imgDict :Dictionary = GameModel.instance.imgDict;
			var bmd :BitmapData;
			if(imgDict[name])
			{
				bmd = imgDict[name] as BitmapData;
			}else
			{
				trace("getBitmapdataByName is null" + name);
			}
			return bmd;
		}
		
		/**
		 * 转换位图到gamemodel中的位图字典中 
		 * @param name
		 * 
		 */		
		public function setBitmapdataInDict(name :String):void
		{
			var imgDict :Dictionary = GameModel.instance.imgDict;
			if(imgDict[name]) return;
			var bmd :BitmapData;
			bmd = McMannager.instance.mcToBMD(name);
			if(!bmd)
			{
				trace("setBitmapdataInDict " + bmd);
				return;
			}
			imgDict[name] = bmd;
		}
		

		
		
		public static function get instance():BitmapManager
		{
			if(_instance == null)
				_instance = new BitmapManager();
			return _instance;
			
		}
		
		
		
	}
}
